/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package chenxu.test.jms.storm;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichBolt;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
/**
 * A generic <code>backtype.storm.topology.IRichBolt</code> implementation
 * for testing/debugging the Storm JMS Spout and example topologies.
 * <p/>
 * For debugging purposes, set the log level of the 
 * <code>backtype.storm.contrib.jms</code> package to DEBUG for debugging
 * output.
 * @author tgoetz
 *
 */
@SuppressWarnings("serial")
public class BcpSplitBolt extends BaseRichBolt {
	private static final Logger LOG = LoggerFactory.getLogger(BcpSplitBolt.class);
	private OutputCollector collector;
	private boolean autoAck = false;
	private boolean autoAnchor = false;
	private Fields declaredFields;
	private String name;
	
	/**
	 * Constructs a new <code>GenericBolt</code> instance.
	 * 
	 * @param name The name of the bolt (used in DEBUG logging)
	 * @param autoAck Whether or not this bolt should automatically acknowledge received tuples.
	 * @param autoAnchor Whether or not this bolt should automatically anchor to received tuples.
	 * @param declaredFields The fields this bolt declares as output.
	 */
	public BcpSplitBolt(String name, boolean autoAck, boolean autoAnchor, Fields declaredFields){
		this.name = name;
		this.autoAck = autoAck;
		this.autoAnchor = autoAnchor;
		this.declaredFields = declaredFields;
	}
	
	public BcpSplitBolt(String name, boolean autoAck, boolean autoAnchor){
		this(name, autoAck, autoAnchor, null);
	}

	@SuppressWarnings("rawtypes")
	public void prepare(Map stormConf, TopologyContext context,
			OutputCollector collector) {
		this.collector = collector;
		
	}

	public void execute(Tuple input) {
		String bcpsWithHeader = input.getString(0);
		String[] split = StringUtils.split(bcpsWithHeader, '\n');
		String header = split[0];
//		LOG.debug("[" + this.name + "] Received message: " + header);
//		LOG.debug("[" + this.name + "] Received message: " + header);
		System.err.println("[" + this.name + "] Received message: " + header);
		Object[] remove = ArrayUtils.remove(split, 0);
		
		for(int i=0; i<remove.length; i++) {
			List<Object> bcpList = new ArrayList<Object>();
//			bcpList.add(i, remove[i]);
			// only emit if we have declared fields.
			String[] bcp = StringUtils.splitPreserveAllTokens((String)remove[i], '\t');
			for(int j=0; j<bcp.length; j++) {
				bcpList.add(bcp[j]);
			}
			if(this.declaredFields != null){
				LOG.debug("[" + this.name + "] emitting: " + bcpList);
				this.collector.emit(bcpList);
				if(this.autoAnchor){
					this.collector.emit(input, bcpList);
				} else{
				}
			}
		}
		
		
		if(this.autoAck){
			LOG.debug("[" + this.name + "] ACKing tuple: " + header);
			this.collector.ack(input);
		}
		
	}

	public void cleanup() {

	}

	public void declareOutputFields(OutputFieldsDeclarer declarer) {
		if(this.declaredFields != null){
			declarer.declare(this.declaredFields);
		}
	}
	
	public boolean isAutoAck(){
		return this.autoAck;
	}
	
	public void setAutoAck(boolean autoAck){
		this.autoAck = autoAck;
	}

}
